import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.sanselan.*;
import org.apache.sanselan.common.IImageMetadata;
import org.apache.sanselan.formats.jpeg.JpegImageMetadata;
import org.apache.sanselan.formats.tiff.TiffDirectory;
import org.apache.sanselan.formats.tiff.TiffField;
import org.apache.sanselan.formats.tiff.TiffImageMetadata;
import org.apache.sanselan.formats.tiff.constants.TagInfo;
import org.apache.sanselan.formats.tiff.constants.TiffConstants;
import org.apache.sanselan.formats.tiff.constants.TiffDirectoryConstants;

public class MetadataExample {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws ImageReadException 
	 */
	public static void main(String[] args) throws ImageReadException, IOException {
		File file = new File("out.jpg"); //WORK WELL WITH PHOTOGRAPHS TAKEN WITH CAMERA
		IImageMetadata metadata = Sanselan.getMetadata(file);
		if (metadata instanceof JpegImageMetadata){
			JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
			System.out.println("file: " + file.getPath());
			
			// print out various interesting EXIF tags.
			printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_XRESOLUTION);
			printTagValue(jpegMetadata, TiffConstants.TIFF_TAG_DATE_TIME);
			printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
			printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_CREATE_DATE);
			printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_ISO);
			printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
			printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_APERTURE_VALUE);
			printTagValue(jpegMetadata, TiffConstants.EXIF_TAG_BRIGHTNESS_VALUE);
			printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LATITUDE_REF);
			printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LATITUDE);
			printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LONGITUDE_REF);
			printTagValue(jpegMetadata, TiffConstants.GPS_TAG_GPS_LONGITUDE);
			
			System.out.println();
			
			// simple interface to GPS data
			TiffImageMetadata exifMetadata = jpegMetadata.getExif();
			if (null != exifMetadata)
			{
				TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
				if (null != gpsInfo)
				{
					String gpsDescription = gpsInfo.toString();
					double longitude = gpsInfo.getLongitudeAsDegreesEast();
					double latitude = gpsInfo.getLatitudeAsDegreesNorth();

					System.out.println("	" + "GPS Description: " + gpsInfo);
					System.out.println("	" + "GPS Longitude (Degrees East): " + longitude);
					System.out.println("	" + "GPS Latitude (Degrees North): " + latitude);
				}
			}
			
			System.out.println("TiffDirectory check");
			
			TiffDirectory td = exifMetadata.findDirectory(TiffDirectoryConstants.DIRECTORY_TYPE_DIR_0);
			System.out.println(td.description() + ": " + td.findField(TiffConstants.EXIF_TAG_MODEL));
			TiffField t = exifMetadata.findField(TiffConstants.EXIF_TAG_MODIFY_DATE);
			System.out.println(t.getTagName() + " -> " + t.getStringValue());
			
			for(Object o : td.getDirectoryEntrys()){
				System.out.println(o);
			}
			
			System.out.println("\njpegMetadata.getItems()");

			ArrayList items = jpegMetadata.getItems();
			for (int i = 0; i < items.size(); i++)
			{
				Object item = items.get(i);
				System.out.println("	" + "item: " + item);
			}

			System.out.println("\nexifMetadata.getDirectories()");
			
			ArrayList dirs = exifMetadata.getDirectories();
			for(Object o : dirs){
				System.out.println(o);
			}
			
		}
	}
	
	//Method to print tag properly
	private static void printTagValue(JpegImageMetadata jpegMetadata,
			TagInfo tagInfo) throws ImageReadException, IOException
	{
		TiffField field = jpegMetadata.findEXIFValue(tagInfo);
		if (field == null)
			System.out.println(tagInfo.name + ": " + "Not Found.");
		else
			System.out.println(tagInfo.name + ": "
					+ field.getValueDescription());
	}

}
